<template>
    <el-dialog title="新 增" :visible.sync="thisAddPage" :before-close="handleClose">
        <el-form ref="blacklistForm" :model="blacklistForm" label-width="135px" :rules="rules">
        <el-form-item label="风险值" prop="riskValue">
            <el-input v-model="blacklistForm.riskValue"></el-input>
        </el-form-item>
        <el-form-item label="风险来源"  prop="source">
            <el-input  v-model="blacklistForm.source" ></el-input>
        </el-form-item>
         <el-form-item label="风险类型"  prop="type">
             <el-input v-model="blacklistForm.type"></el-input>
        </el-form-item>
        <el-form-item label="是否失效"  prop="isValid">
             <el-radio-group v-model="blacklistForm.isValid" >
                <el-radio border label="1" >有效</el-radio>
                <el-radio border label="0" >无效</el-radio>
            </el-radio-group>
        </el-form-item>
        <!-- <el-form-item label="失效时间"  prop="invalidTime">
            <el-checkbox v-model="blacklistForm.invalidTime"></el-checkbox>
        </el-form-item> -->
        <el-form-item>
            <el-button type="primary" @click="onSubmit('blacklistForm')">创 建</el-button>
            <el-button @click="resetForm('blacklistForm')">重 置</el-button>
            <el-button @click="change">取 消</el-button>
        </el-form-item>
        </el-form>
    </el-dialog>
</template>
<script>
import { SaveBlacklist } from '@api/blacklist/blacklist'
  export default {
    data() {
        
      return {
          thisAddPage: this.addPage,
        blacklistForm: {
          id: '',
          riskValue: '',
          source: '',
          type:'',
          isValid: '',
          invalidTime: ''
        },
        rules: {
          riskValue: [
            { required: true, message: '请输入风险值', trigger: 'blur' },
            { min: 2, max: 20, message: '长度在 2 到 32 个字符', trigger: 'blur' }
          ],
          type: [
            { required: true, message: '请输入风险类型', trigger: 'blur' }
          ] 
        
        }
      }
    },
    /**双向绑定 start */
    props: ["addPage"],
    watch: {
        addPage(val) {
            this.thisAddPage = val;//②监听外部对props属性result的变更，并同步到组件内的data属性myResult中
        },
        thisAddPage(val){
            //xxcanghai 小小沧海 博客园
             this.$emit("on-addPage-change",val);//③组件内对myResult变更后向外部发送事件通知
        }
    },
  
    methods: {
        change() {
            this.thisAddPage = !this.thisAddPage;
            this.$refs['departmentForm'].resetFields();
        },
          /**双向绑定 end */

        onSubmit(formName) {
            this.$refs[formName].validate((valid) => {
            if (valid) {
                this.loading = true
                SaveBlacklist(this.blacklistForm).then(res=>{
                    this.$message({
                    message: '创建成功',
                    type: 'success'
                    });
                     this.$emit("on-loadlist-change");//③组件内对myResult变更后向外部发送事件通知
                     this.loading = false
                     this.change()
                }).catch(err=>{
                    console.log('err', err)
                    this.loading = false
                })
               
            } else {
                console.log('error submit!!');
                return false;
            }
            });
        },
        resetForm(formName) {
          this.$refs[formName].resetFields();
         },
        handleClose(done){
            this.$confirm('确定关闭吗').then(() => {
                // function(done)，done 用于关闭 Dialog
                done();
            }).catch(() => {
               // console.log("点击确定时触发");
            });
        }
    },
    created(){
        //console.log("created");
    },
    mounted(){
        //console.log("mounted");
    },
    computed:{
       
    }
  }
</script>